#region Using directives
using System;
using System.Data ;

using WP.DB;
using WP.MF.DataEntity; 
using WP.Commons;
#endregion

namespace WP.MF.DataAccess
{

	/*
	 * Created by SmartCreateCode Tool
	 * At 4/6/2005
	 */

    public class MPeriodDAL
	{
		public MPeriodDAL()
		{
		}
		#region Select by Key
		public MPeriodDEL SelectMPeriodByKey(DBAccess dBAccess, int iRecId)
		{
			MPeriodDEL mPeriodDEL=new MPeriodDEL();
			string strSql=string.Empty;
			object[] oValues;

			strSql=@"SELECT RecId,ModuleName,Period,convert(varchar,stPrdDate,101) as StPrdDate,convert(varchar,EnPrdDate,101) as EnPrdDate,
				convert(varchar,PreYrStPrdDate,101) as PreYrStPrdDate,convert(varchar,PreYrEnPrdDate,101) as PreYrEnPrdDate,
				convert(varchar,PrePreYrStPrdDate,101) as PrePreYrStPrdDate,convert(varchar,PrePreYrEnPrdDate,101) as PrePreYrEnPrdDate,CurPrd  FROM MPeriod
				WHERE RecId={0}";

			oValues=new object[1];
			oValues[0]=iRecId;
			strSql=string.Format(strSql,oValues);
			dBAccess.GetDataSet(strSql,MPeriodDEL.TABLENAME_MPERIOD,mPeriodDEL);
			return mPeriodDEL;
		}
		#endregion

		#region Insert table by DataRow
		public void InsertMPeriod(DBAccess dBAccess,MPeriodDEL mPeriodDEL)
		{
			string strSql=string.Empty;
			int iIndex=0;
			object[] oValues;

			
			foreach(DataRow DR in mPeriodDEL.Tables[MPeriodDEL.TABLENAME_MPERIOD].Rows)
			{
				strSql=@"INSERT INTO MPeriod  (ModuleName,Period,StPrdDate,EnPrdDate,
				PreYrStPrdDate,PreYrEnPrdDate,PrePreYrStPrdDate,PrePreYrEnPrdDate,CurPrd ) VALUES ( 
				'{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}',{8} )";

				iIndex=0;
				oValues=new object[9];
				oValues[iIndex++]=DR[MPeriodDEL.MODULENAME].ToString();
				oValues[iIndex++]=DR[MPeriodDEL.PERIOD].ToString();
				oValues[iIndex++]=DR[MPeriodDEL.STPRDDATE].ToString();
				oValues[iIndex++]=DR[MPeriodDEL.ENPRDDATE].ToString();
				oValues[iIndex++]=DR[MPeriodDEL.PREYRSTPRDDATE].ToString();
				oValues[iIndex++]=DR[MPeriodDEL.PREYRENPRDDATE].ToString();
				oValues[iIndex++]=DR[MPeriodDEL.PREPREYRSTPRDDATE].ToString();
				oValues[iIndex++]=DR[MPeriodDEL.PREPREYRENPRDDATE].ToString();
				oValues[iIndex++]=DR[MPeriodDEL.CURPRD].ToString();

				strSql=string.Format(strSql,oValues);

				dBAccess.ExecuteSql(strSql);
			}
		}
		#endregion

		#region Update table by DataRow
		public void UpdateMPeriodForSepcialModule(DBAccess dBAccess,MPeriodDEL mPeriodDEL)
		{
			string strSql=string.Empty;
			string strModuleName="";
			DataView DV=null;

			strModuleName=mPeriodDEL.Tables[MPeriodDEL.TABLENAME_MPERIOD].Rows[0][MPeriodDEL.MODULENAME].ToString();
			if(strModuleName.IndexOf("IM")>=0 || strModuleName.IndexOf("PO")>=0 || strModuleName.IndexOf("OE")>=0 || strModuleName.IndexOf("RM")>=0)
			{
				DV=new DataView(mPeriodDEL.Tables[MPeriodDEL.TABLENAME_MPERIOD]);
				DV.RowFilter="Period='CP'";
					
				strSql="UPDATE MPeriod Set CurPrd='"+DV[0]["CurPrd"].ToString()+"' WHERE Period='CP' and (ModuleName in ('PO','OE','IM','RM'))";
				dBAccess.ExecuteSql(strSql);


				strSql = @"Update mperiod set StPrdDate=m.StPrdDate,EnPrdDate=m.EnPrdDate,PreYrStPrdDate=m.PreYrStPrdDate,PreYrEnPrdDate=m.PreYrEnPrdDate,PrePreYrStPrdDate=m.PrePreYrStPrdDate,
						PrePreYrEnPrdDate=m.PrePreYrEnPrdDate from (select Period,StPrdDate,EnPrdDate,PreYrStPrdDate,PreYrEnPrdDate,PrePreYrStPrdDate,PrePreYrEnPrdDate from mperiod 
						where ModuleName='" + strModuleName + @"' and Period<>'CP') m where mperiod.Period=m.Period 
						and mperiod.ModuleName in ('OE','PO','IM','RM') and mperiod.ModuleName<>'" + strModuleName + "'";

				dBAccess.ExecuteSql(strSql);
			}
		}
		#endregion

		#region Delete table by Key
		public void DeleteMPeriod(DBAccess dBAccess,string strModuleName)
		{
			string strSql=string.Empty;

			strSql=@"DELETE FROM mPeriod WHERE ModuleName='"+strModuleName+"'";

			dBAccess.ExecuteSql(strSql);
		}
		#endregion

		public string GetCurPeriodByImAndCP(DBAccess dBAccess,string Im,string Cp)
		{
			string CurPeriod = "";
			string sql = "Select CurPrd From mPeriod Where ModuleName='" + Im + "' And Period='"+Cp+"'";
			CurPeriod = dBAccess.GetValue(sql);
			return CurPeriod;
		}
		
		#region GetPeriodByModuleName: GetPeriod into By ModuleName
		public MPeriodDEL GetPeriodByModuleName(DBAccess dBAccess,SystemModules systemModules)
		{
			string strModduleName="";
			MPeriodDEL mPeriodDEL=new MPeriodDEL();
			if(systemModules==SystemModules.MF)
				strModduleName="M";
			else
				strModduleName=systemModules.ToString();

			string sql = @"Select RecId,ModuleName,Period,convert(varchar,stPrdDate,101) as StPrdDate,convert(varchar,EnPrdDate,101) as EnPrdDate,
				convert(varchar,PreYrStPrdDate,101) as PreYrStPrdDate,convert(varchar,PreYrEnPrdDate,101) as PreYrEnPrdDate,
				convert(varchar,PrePreYrStPrdDate,101) as PrePreYrStPrdDate,convert(varchar,PrePreYrEnPrdDate,101) as PrePreYrEnPrdDate,CurPrd  FROM mPeriod Where ModuleName='" + strModduleName + "'";

			dBAccess.GetDataSet(sql,MPeriodDEL.TABLENAME_MPERIOD,mPeriodDEL);
			return mPeriodDEL;
		}	
		#endregion


	}
}
